home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / tools / cycol.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  285 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    cycol - 
  19.  *        Display a pallete of colors.  A new palette may be selected 
  20.  *        by pointing with the mouse and clicking the left mouse button.
  21.  *
  22.  *                David Ratcliffe - 1987
  23.  */
  24. #include "stdio.h"
  25. #include "gl.h"
  26. #include "device.h"
  27. #include "port.h"
  28.  
  29. #define STANDSTILL  0
  30. #define LEFT        1
  31. #define RIGHT       2
  32. #define MAXCOL   4096
  33.  
  34. short Start, End;                          /* indices of color range       */
  35. short ro[MAXCOL], go[MAXCOL], bo[MAXCOL];  /* original color vals          */
  36. short ri[MAXCOL], gi[MAXCOL], bi[MAXCOL];  /* initial "new" color vals     */
  37. short rc[MAXCOL], gc[MAXCOL], bc[MAXCOL];  /* current color vals           */
  38. long sx, sy;                         /* x and y lengths of sides of window */
  39. long ox, oy;                         /* x and y screen origin of window    */
  40. long curscrnx, curscrny;             /* current x/y screen position        */
  41. long prescrnx, prescrny;             /* previous x/y screen position       */
  42. int deltax;                          /* change of x delimits "speed" of LM */
  43. int inwindow;                        /* flag for LEFTMOUSE decision making */
  44. int justleftwindow;                  /* flag for LEFTMOUSE decision making */
  45. int ncolors;                         /* number of colors in current range  */
  46. int menu;                 /* the pop-up menu            */
  47.  
  48. main(argc,argv)
  49. int argc;
  50. char **argv;
  51. {
  52.     short val;
  53.     static short pos1, pos2;
  54.     int dorot, totalrot, currentrot;
  55.     float downxpos, curxpos, fgetmousex();
  56.  
  57.     if (argc==3) {
  58.         Start = (short) atoi(argv[1]);
  59.         pos2 = End = (short) atoi(argv[2]);
  60.     } else {
  61.         Start = 128;
  62.         pos2 = End = 255;
  63.     }
  64.     ncolors = End - Start + 1;
  65.     justleftwindow = FALSE;
  66.     winopen("cycol");
  67.     glcompat(GLC_SOFTATTACH,TRUE);
  68.     menu = defpup("cycol %t|restore range|restore map|clean exit");
  69.     getorigin(&ox, &oy);
  70.     getsize(&sx, &sy);
  71.     qdevice(LEFTMOUSE);
  72.     qdevice(MENUBUTTON);
  73.     get_cmap();
  74.     readmap();
  75.     displaymap();
  76.     while (1) {
  77.         switch(qread(&val)) {
  78.             case REDRAW:
  79.         reshapeviewport();
  80.                 getorigin(&ox, &oy);
  81.                 getsize(&sx, &sy);
  82.                 displaymap();
  83.                 break;
  84.             case MENUBUTTON:
  85.         if(val) {
  86.             switch(dopup(menu)) {
  87.             case 1:
  88.                         reset_colors();
  89.                 break;
  90.             case 2:
  91.                 restore_cmap();
  92.                 break;
  93.             case 3:
  94.                 restore_cmap();
  95.                 gexit();
  96.                 exit(0);
  97.                 break;
  98.             }
  99.         }
  100.                 break;
  101.             case LEFTMOUSE:
  102.                 inwindow = findcurspos();
  103.                 if (inwindow) {
  104.                     downxpos = fgetmousex();
  105.                     currentrot = 0;
  106.                     while (getbutton(LEFTMOUSE)) {
  107.                         curxpos = fgetmousex();
  108.                         totalrot = ((int)((ncolors*(curxpos-downxpos)) +10000.5)) -10000;
  109.                         dorot = totalrot - currentrot;
  110.                         if (dorot != 0)
  111.                 shiftmap(dorot);
  112.             currentrot = totalrot;
  113.                     }
  114.                     justleftwindow = TRUE;
  115.         } else if ((!val) && (!justleftwindow)) {
  116.                     pos1 = getapixel(getvaluator(MOUSEX), getvaluator(MOUSEY));
  117.                     maprange(pos1, pos2);
  118.                     pos2 = pos1;
  119.                 } else 
  120.                     justleftwindow = FALSE; /* just left the cycol window so 
  121.                                                make the next go round correct-
  122.                                                ly check for new color values */
  123.                 displaymap();
  124.                 break;
  125.             default:
  126.                 break;
  127.         }
  128.     }
  129. }
  130.  
  131. maprange(pos1,pos2)
  132. short pos1, pos2;
  133. {
  134.     Start = MIN(pos1, pos2);
  135.     End   = MAX(pos1, pos2);
  136.     ncolors = End - Start + 1;
  137.     displaymap();
  138.     readmap();
  139. }
  140.  
  141. displaymap()
  142. {
  143.     int i;
  144.  
  145.     ortho2((float)Start,(float)End+1,0.0,1.0);
  146.     for (i=Start; i<=End; i++) {
  147.         color(i);
  148.         rectfi(i,0,i+1,1);
  149.     }
  150. }
  151.  
  152. findcurspos()
  153. {
  154.     long x, y;
  155.  
  156.     inwindow = FALSE;
  157.     x = getvaluator(MOUSEX);
  158.     y = getvaluator(MOUSEY);
  159.     if ((ox < x) && (x < (ox + sx)) 
  160.      && (oy < y) && (y < (oy + sy)))
  161.         inwindow = TRUE;
  162.     return(inwindow);
  163. }
  164.  
  165.  
  166. shiftmap(n)
  167. int n;
  168. {
  169.     int i;
  170.  
  171.     if (n>0) {
  172.         for (i=0; i<n; i++)
  173.             shiftone(RIGHT);
  174.     } 
  175.     else if (n<0) {
  176.         n = -n;
  177.         for (i=0; i<n; i++)
  178.             shiftone(LEFT);
  179.     } 
  180.     writemap();
  181. }
  182.  
  183. shiftone(direction) 
  184. int direction;
  185. {
  186.     short cind;
  187.     short tmpr, tmpg, tmpb;                      /* temp rgb components */
  188.           
  189.     if (direction == RIGHT) {
  190.         tmpr = rc[End];
  191.         tmpg = gc[End];
  192.         tmpb = bc[End];
  193.         for (cind=End-1; cind>=Start; cind--) {
  194.             rc[cind+1] = rc[cind];
  195.             gc[cind+1] = gc[cind];
  196.             bc[cind+1] = bc[cind];
  197.         }
  198.         rc[Start] = tmpr;
  199.         gc[Start] = tmpg;
  200.         bc[Start] = tmpb;
  201.     } else if (direction == LEFT) {
  202.         tmpr = rc[Start];
  203.         tmpg = gc[Start];
  204.         tmpb = bc[Start];
  205.         for (cind=Start+1; cind<=End; cind++) {
  206.             rc[cind-1] = rc[cind];
  207.             gc[cind-1] = gc[cind];
  208.             bc[cind-1] = bc[cind];
  209.         }
  210.         rc[End] = tmpr;
  211.         gc[End] = tmpg;
  212.         bc[End] = tmpb;
  213.     }
  214. }
  215.  
  216. writemap()                       /* update current range to current colors */
  217. {
  218.     short cind;
  219.  
  220.     for (cind=Start; cind<=End; cind++)
  221.         mapcolor(cind,rc[cind],gc[cind],bc[cind]); 
  222. }
  223.  
  224. readmap()
  225. {
  226.     short cind;
  227.  
  228.     for (cind=Start; cind<=End; cind++) {
  229.         getmcolor(cind,&rc[cind],&gc[cind],&bc[cind]);/* fill current range */
  230.     ri[cind] = rc[cind];
  231.     gi[cind] = gc[cind];
  232.     bi[cind] = bc[cind];
  233.     }
  234. }
  235.  
  236. reset_colors() 
  237. {
  238.     short cind;
  239.  
  240.     for (cind=Start; cind<=End; cind++) 
  241.         mapcolor(cind, ri[cind], gi[cind], bi[cind]);
  242.     displaymap();
  243. }
  244.  
  245. get_cmap()      /* save original colormap--this will never get overwritten */
  246. {
  247.     short cind;
  248.     int mapcolors;
  249.  
  250.     mapcolors = (1<<getplanes());
  251.     for (cind=0; cind<mapcolors; cind++) 
  252.         getmcolor(cind,&ro[cind],&go[cind],&bo[cind]); 
  253. }
  254.  
  255. restore_cmap() 
  256. {
  257.     short cind;
  258.     int mapcolors;
  259.  
  260.     mapcolors = (1<<getplanes());
  261.     for (cind=0; cind<mapcolors; cind++) 
  262.         mapcolor(cind, ro[cind], go[cind], bo[cind]);
  263. }
  264.  
  265. float fgetmousex() 
  266. {
  267.     long x;
  268.     float fx;
  269.  
  270.     x = getvaluator(MOUSEX);
  271.     if (x < ox) {
  272.         fx = 0.0;
  273.     }
  274.     else if (x > (ox+sx)) {
  275.         fx = 1.0;
  276.     } else {
  277.         fx = (float) (x-ox) / (float) sx;
  278.         if (fx>1.0)
  279.             fx = 1.0;
  280.         else if (fx < 0.0)
  281.             fx = 0.0;
  282.     }
  283.     return (fx);
  284. }
  285.